package com.panshi.controller.user;

import com.panshi.api.UserApi;
import com.panshi.po.UserPO;
import com.panshi.vo.UserUpdateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class UpdateUserController {

    @Autowired
    private UserApi userApi;

    @GetMapping("update")
    public String update(@RequestParam Integer id, Model model){


        UserPO userPO= userApi.findUserById(id).getData();
        log.info("userPO==========={}",userPO);
        model.addAttribute("user",userPO);

        return  "user/user-update";
    }


    @PostMapping("user-update")
    @ResponseBody
    public Map<String,Object>  updateUser( UserUpdateVO userPO){

        log.info("userPOjjj=========",userPO);
        Map<String,Object> map = new HashMap<>();
        userPO.setUpdatedate(new Date());

        Integer integer= userApi.userUpdate(userPO).getData();

        if(integer == 0){
            map.put("success",false);
            map.put("message","修改失败，请重新修改");
        }else if(integer == 1){
            map.put("success",true);
            map.put("message","修改成功");
        }
        return map;
    }

    /**
     * 删除
     * @param userPO
     * @return
     */
    @PostMapping("user-delete")
    @ResponseBody
    public Map<String,Object> deleteUser( UserUpdateVO userPO){
        Map<String,Object> map = new HashMap<>();
        userPO.setUpdatedate(new Date());

        Integer integer= userApi.userUpdate(userPO).getData();

        if(integer == 0){
            map.put("success",false);
            map.put("message","删除失败，请重新删除");
        }else if(integer == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }
        return map;
    }


}
